#include "mainwindow.h"
#include "launchdialog.h"
#include "logindialog.h"
#include <QApplication>

#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include <QDebug>
#include <QDesktopWidget>
#include <QtWidgets>


void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();
    QString text;
    switch (type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        break;
    case QtWarningMsg:
        text = QString("Warning:");
        break;
    case QtCriticalMsg:
        text = QString("Critical:");
        break;
    case QtFatalMsg:
        text = QString("Fatal:");
        break;
    case QtInfoMsg:
        text = QString("Info:");
        break;
    }
	QFileInfo fileInfo(context.file);
#ifdef QT_NO_DEBUG
 //   QString context_info = QString("[%1][%2]").arg(fileInfo.fileName()).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("%1 %2 %3").arg(text).arg(current_date).arg(msg);

    QFile file("log.txt");
	QFileInfo fileInfo2(file);
	qint64 size = fileInfo2.size();
	if (size > 1024 * 1024 * 50)
	{
		QFile::remove("log.txt");
	}

	file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << endl;
    file.flush();
    file.close();
#else
	QString context_info = QString("[%1][%2]").arg(fileInfo.fileName()).arg(context.line);
	QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
	QString current_date = QString("(%1)").arg(current_date_time);
	QString message = QString("%1 %2 %3 %4\n").arg(text).arg(context_info).arg(current_date).arg(msg);
	OutputDebugStringA(message.toStdString().data());
	//qDebug() << message << endl;
#endif
    mutex.unlock();
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString strKey = QCoreApplication::organizationName() + QCoreApplication::applicationName();

    QSharedMemory sharedMemory(strKey);
    if (!sharedMemory.create(512, QSharedMemory::ReadWrite))
    {
        QMessageBox::information(nullptr, QStringLiteral("Warning"), QStringLiteral("Software is running"));
        exit(0);
    }
	qInstallMessageHandler(outputMessage);


    QFile styleFile(QStringLiteral(":/css/blue.css"));
    if(styleFile.open(QFile::ReadOnly)){
        QString qss = styleFile.readAll();
        a.setStyleSheet(qss);
        a.setPalette(QPalette(QColor("#e0e0e0")));
        styleFile.close();
    }
    else{
        qDebug()<<"styleFile Loading error";
    }
	
    {
        QString dir_str = QString("%1/Config").arg(QCoreApplication::applicationDirPath());
        //  qDebug()<<dir_str;
        //检查目录是否存在，若不存在则新建
        QDir dir;
        if (!dir.exists(dir_str))
        {
            dir.mkpath(dir_str);
          //  qDebug() << "新建目录是否成功" <<dir_str<< res;
        }
    }

    {
        QString dir_str = QString("%1/Results").arg(QCoreApplication::applicationDirPath());
      //  qDebug()<<dir_str;
        //检查目录是否存在，若不存在则新建
        QDir dir;
        if (!dir.exists(dir_str))
        {
            dir.mkpath(dir_str);
          //  qDebug() << "新建目录是否成功" <<dir_str<< res;
        }
    }

    {
        QString dir_str = QString("%1/TempImages").arg(QCoreApplication::applicationDirPath());
      //  qDebug()<<dir_str;
        //检查目录是否存在，若不存在则新建
        QDir dir;
        if (!dir.exists(dir_str))
        {
            dir.mkpath(dir_str);
           // qDebug() << "新建目录是否成功" <<dir_str<< res;
        }


        QString currentdate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
        QString dir_str2 = QString("%1/%2").arg(dir_str).arg(currentdate);
        if (!dir.exists(dir_str2))
        {
            dir.mkpath(dir_str2);
          //  qDebug() << "新建目录是否成功" <<dir_str<< res;
        }
    }

    // LoginDialog dlg;
 /*   LaunchDialog dlg;
    dlg.setWindowIcon(QIcon(":/images/ic.png"));
    if(dlg.exec()==QDialog::Accepted){
        MainWindow w;
        w.show();
        w.move((a.desktop()->width() - w.width()) / 2, (a.desktop()->height() - w.height()) / 2);
        return a.exec();
    }else{
        return 0;
    }*/
	QString n = QCoreApplication::applicationName();
	QString v = QCoreApplication::applicationVersion();
	qInfo() <<"-------------"<< n << v <<"start-------------";
	QString appName = QCoreApplication::applicationName();
	QSettings app(appName, "Debug");
	QDateTime time = QDateTime::currentDateTime();
	QString currentdate = time.toString("yyyy-MM-dd_hh-mm-ss");
	//QString currentime = time.toString("hh_mm_ss");
	app.setValue("StartTime", currentdate);
	MainWindow w;
	w.show();
	w.move((a.desktop()->width() - w.width()) / 2, (a.desktop()->height() - w.height()) / 2);
	return a.exec();
  //  return a.exec();
}
